package com.zy.mc_module.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.SafeKeyGenerator;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.EmptySignature;
import com.zy.mc_module.R;

import java.io.File;
import java.io.IOException;

/**
 * Created on 18/11/28
 * Glide图片加载库
 */
public class GlideUtil {


    /**
     * 加载普通网络图片,设置圆角
     *
     * @param imgUrl
     * @param imageView
     */
    public static void displayRoundedImage(ImageView imageView, String imgUrl, int radius) {
        if (null != imageView && null != imageView.getContext()) {
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .placeholder(R.drawable.img_default)
                    .error(R.drawable.img_default)
                    .transform(new GlideRoundUtils(radius, GlideRoundUtils.CornerType.TOP));
            try {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * 加载视屏第一帧图
     * @param imageView
     * @param videoPath
     */
    public static void displayVideoImg(ImageView imageView, String videoPath) {
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .centerCrop()
                .placeholder(R.drawable.img_default)
                .error(R.drawable.img_default)
                .fallback(R.drawable.img_default);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(videoPath)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载普通网络图片
     *
     * @param imgUrl
     * @param imageView
     */
    public static void displayOriginalImage(ImageView imageView, String imgUrl) {
        RequestOptions options = new RequestOptions()
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .placeholder(R.drawable.img_default)
                .error(R.drawable.img_default)
                .fallback(R.drawable.img_default);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载普通网络图片
     *
     * @param imgUrl
     * @param imageView
     */
    public static void displayImage(ImageView imageView, String imgUrl) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.img_default)
                .error(R.drawable.img_default)
                .fallback(R.drawable.img_default);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载普通网络图片
     *
     * @param imgUrl
     * @param imageView
     */
    public static void displayImage(ImageView imageView, String imgUrl, int errorImg) {
        RequestOptions options = new RequestOptions()
                .placeholder(errorImg)
                .error(errorImg)
                .fallback(errorImg);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void displayFromWeb(ImageView imageView, String imgUrl, int errorImg) {
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .placeholder(errorImg)
                .error(errorImg)
                .fallback(errorImg);
        Glide.with(imageView.getContext())
                .load(imgUrl)
                .apply(options)
                .into(imageView);
    }
    //4.0
    public static File getCacheFile2(String url, Context context) {
        DataCacheKey dataCacheKey = new DataCacheKey(new GlideUrl(url), EmptySignature.obtain());
        SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
        String safeKey = safeKeyGenerator.getSafeKey(dataCacheKey);
        try {
            int cacheSize = 100 * 1000 * 1000;
            DiskLruCache diskLruCache = DiskLruCache.open(new File(context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR), 1, 1, cacheSize);
            DiskLruCache.Value value = diskLruCache.get(safeKey);
            if (value != null) {
                return value.getFile(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加载普通网络图片
     *
     * @param imgUrl
     * @param imageView
     */
    public static void displayImage(int radius, ImageView imageView, String imgUrl, int errorImg) {
        RequestOptions options = new RequestOptions()
                .placeholder(errorImg)
                .error(errorImg)
                .transform(new RoundedCornersTransformation(radius, 0))
                .fallback(errorImg);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void displayImage(int radius, RoundedCornersTransformation.CornerType cornerType, ImageView imageView, String imgUrl, int errorImg) {
        if (errorImg == 0) errorImg = R.drawable.img_default;
        RequestOptions options = new RequestOptions()
                .placeholder(errorImg)
                .error(errorImg)
                .transform(new RoundedCornersTransformation(radius, 0, cornerType))
                .fallback(errorImg);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载Bitmap
     **/
    public static void displayImage(ImageView imageView, Bitmap bitmap, int errorImg) {
        RequestOptions options = new RequestOptions()
                .placeholder(errorImg)
                .error(errorImg)
                .fallback(errorImg);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(bitmap)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 加载本地图片
     * @param imageView
     * @param imgId
     */
    public static void displayImage(ImageView imageView, int imgId) {
        RequestOptions options = new RequestOptions()
                .centerInside()//防止加载本地图片被放大
                .placeholder(R.drawable.img_default)
                .error(R.drawable.img_default)
                //.encodeFormat(Bitmap.CompressFormat.WEBP)
                //.format(DecodeFormat.PREFER_ARGB_8888)
                .fallback(R.drawable.img_default);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imgId)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片选择器的
     **/
    public static void displayImagePicker(ImageView imageView, String imageUrl, int width, int height) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.img_default)
                .error(R.drawable.img_default)
                .fallback(R.drawable.img_default)
                .override(width, height);
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .load(imageUrl)
                        .apply(options)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 加载网络视屏第一帧图
     * @param imageView
     * @param videoPath
     */
    public static void displayNetVideoImg(ImageView imageView, String videoPath) {
        try {
            if (null != imageView && null != imageView.getContext()) {
                Glide.with(imageView.getContext())
                        .setDefaultRequestOptions(
                                new RequestOptions()
                                        .frame(1000000)
                                        .centerCrop()
                                        .error(R.drawable.img_default)
                                        .placeholder(R.drawable.img_default))
                        .load(videoPath)
                        .into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
